import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import BlankLayout from '../views/RouterView.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/dashboard',
    component: Home,
    children: [
      // 系统首页
      {
        path: '/dashboard',
        name: 'dashboard',
        meta: { title: '系统首页', icon: 'el-icon-lx-home' },
        component: () => import('../views/Dashboard.vue'),
      },

      // 系统管理
      {
        path: '/system',
        name: 'system',
        redirect: '/system/advertPic',
        component: BlankLayout,
        meta: {
          title: '系统管理',
          icon: 'el-icon-lx-settings',
        },
        children: [
          {
            path: '/advertPic',
            name: 'advertPic',
            meta: { title: '广告图片' },
            component: () => import('../views/system/advertPic'),
          },
          {
            path: '/usersMessage',
            name: 'usersMessage',
            meta: { title: '公告信息中心' },
            component: () => import('../views/system/usersMessage'),
          },
          {
            path: '/appConfig',
            name: 'appConfig',
            meta: { title: 'APP配置', hide: true },
            component: () => import('../views/system/appConfig'),
          },
          {
            path: '/sysConfig',
            name: 'sysConfig',
            meta: { title: '系统配置', hide: true },
            component: () => import('../views/system/sysConfig'),
          },
          {
            path: '/dayTask',
            name: 'dayTask',
            meta: { title: '任务配置', hide: true },
            component: () => import('../views/system/dayTask'),
          },
          {
            path: '/tripartiteLink',
            name: 'tripartiteLink',
            meta: { title: '三方链接', hide: true },
            component: () => import('../views/system/tripartiteLink'),
          },
          {
            path: '/mobileCode',
            name: 'mobileCode',
            meta: { title: '用户手机验证码', hide: true },
            component: () => import('../views/system/mobileCode'),
          },
        ],
      },

      // 用户管理
      {
        path: '/user-manage',
        name: 'userManage',
        redirect: '/userControl',
        component: BlankLayout,
        meta: {
          title: '用户管理',
          icon: 'el-icon-lx-group',
        },
        children: [
          {
            path: '/userControl',
            name: 'userControl',
            meta: { title: '用户管理' },
            component: () => import('../views/user/userControl'),
          },
          {
            path: '/housekeeperClass',
            name: 'housekeeperClass',
            meta: { title: '管家列表' },
            component: () => import('../views/housekeeper/housekeeper'),
          },
          {
            path: '/powerAudit',
            name: 'powerAudit',
            meta: { title: '商家审核' },
            component: () => import('../views/user/powerAudit'),
          },
          {
            path: '/merchantList',
            name: 'merchantList',
            meta: { title: '商家列表' },
            component: () => import('../views/merchant/merchant'),
          },
          {
            path: '/authentication',
            name: 'authentication',
            meta: { title: '实名认证审核' },
            component: () => import('../views/user/authentication'),
          },
          {
            path: '/userAddress',
            name: 'userAddress',
            meta: { title: '用户地址管理', hide: true },
            component: () => import('../views/user/address'),
          },
          {
            path: '/userAutonym',
            name: 'userAutonym',
            meta: { title: '用户实名认证信息', hide: true },
            component: () => import('../views/user/autonym'),
          },
          {
            path: '/subscribersAgent',
            name: 'subscribersAgent',
            meta: { title: '后台获取用户代理', hide: true },
            component: () => import('../views/user/subscribersAgent'),
          },
        ],
      },

      // 个人中心（隐藏）
      {
        path: '/user',
        name: 'user',
        meta: { title: '个人中心', hide: true },
        component: () => import('../views/user/index'),
      },
      // 个人中心（隐藏）
      {
        path: '/icon',
        name: 'icon',
        meta: { title: '个人中心' },
        component: () => import('../views/Icon'),
      },

      // 等级管理
      {
        path: '/level',
        name: 'level',
        redirect: '/usersMemberConfig',
        component: BlankLayout,
        meta: {
          title: '等级管理',
          icon: 'el-icon-lx-crown',
        },
        children: [
          {
            path: '/usersMemberConfig',
            name: 'usersMemberConfig',
            meta: { title: 'VIP等级配置' },
            component: () => import('../views/level/usersMemberConfig'),
          },
          {
            path: '/usersTeamConfig',
            name: 'usersTeamConfig',
            meta: { title: '团队等级配置' },
            component: () => import('../views/level/usersTeamConfig'),
          },
          {
            path: '/usersTierConfig',
            name: 'usersTierConfig',
            meta: { title: '合伙人等级配置' },
            component: () => import('../views/level/usersTierConfig'),
          },
        ],
      },

      // 门店管理
      {
        path: '/store-manage',
        name: 'storeManage',
        redirect: '/store',
        component: BlankLayout,
        meta: {
          title: '门店管理',
          icon: 'el-icon-lx-shop',
        },
        children: [
          {
            path: '/store',
            name: 'store',
            meta: { title: '门店管理' },
            component: () => import('../views/store/store'),
          },
          {
            path: '/storeClass',
            name: 'storeClass',
            meta: { title: '门店分类' },
            component: () => import('../views/store/class'),
          },
          {
            path: '/storeOrder',
            name: 'storeOrder',
            meta: { title: '商家订单管理' },
            component: () => import('../views/store/order'),
          },
          {
            path: '/storeConfig',
            name: 'storeConfig',
            meta: { title: '商户配置', hide: true },
            component: () => import('../views/store/config'),
          },
        ],
      },

      // 自营商城
      {
        path: '/zyMall',
        name: 'zyMall',
        redirect: '/zyMallClass',
        component: BlankLayout,
        meta: {
          title: '自营商城',
          icon: 'el-icon-lx-goods',
        },
        children: [
          {
            path: '/zyMallClass',
            name: 'zyMallClass',
            meta: { title: '自营商城分类' },
            component: () => import('../views/zyMall/class'),
          },
          {
            path: '/zyMallCommodity',
            name: 'zyMallCommodity',
            meta: {
              title: '自营商城商品',
            },
            component: () => import('../views/zyMall/commodity.vue'),
          },
          {
            path: '/zyMallOrder',
            name: 'zyMallOrder',
            meta: {
              title: '自营商城订单',
            },
            component: () => import('../views/zyMall/order'),
          },
          {
            path: '/zyMallPartition',
            name: 'zyMallPartition',
            meta: { title: '自营分区' },
            component: () => import('../views/zyMall/partition'),
          },
          {
            path: '/zyMallRefund',
            name: 'zyMallRefund',
            meta: {
              title: '自营商城退款',
            },
            component: () => import('../views/zyMall/refund'),
          },
          {
            path: '/freight',
            name: 'freight',
            meta: { title: '运费模板' },
            component: () => import('../views/freight/freight'),
          },
          {
            path: '/selfPickingCard',
            name: 'selfPickingCard',
            meta: { title: '自提卡', hide: true },
            component: () => import('../views/zyMall/selfPickingCard'),
          },
        ],
      },

      // 管家管理
      // {
      //   path: "/housekeeper",
      //   name: "housekeeper",
      //   redirect: "/housekeeperClass",
      //   component: BlankLayout,
      //   meta: {
      //     title: "管家管理",
      //     icon: "el-icon-user",
      //   },
      //   children: [
      //     {
      //       path: "/housekeeperClass",
      //       name: "housekeeperClass",
      //       meta: { title: "管家列表" },
      //       component: () => import("../views/housekeeper/housekeeper"),
      //     },
      //   ],
      // },
      // 商家管理
      // {
      //   path: "/merchant",
      //   name: "merchant",
      //   redirect: "/merchantList",
      //   component: BlankLayout,
      //   meta: {
      //     title: "商家管理",
      //     icon: "el-icon-lx-shop",
      //   },
      //   children: [
      //     {
      //       path: "/merchantList",
      //       name: "merchantList",
      //       meta: { title: "商家列表" },
      //       component: () => import("../views/merchant/merchant"),
      //     },
      //   ],
      // },
      // 释放管理
      {
        path: '/release',
        name: 'release',
        redirect: '/releaseConfig',
        component: BlankLayout,
        meta: {
          title: '释放管理',
          icon: 'el-icon-lx-tag',
        },
        children: [
          {
            path: '/releaseConfig',
            name: 'releaseConfig',
            meta: { title: '释放配置' },
            component: () => import('../views/release/config'),
          },
          {
            path: '/releaseOrder',
            name: 'releaseOrder',
            meta: { title: '释放订单' },
            component: () => import('../views/release/order'),
          },
          {
            path: '/releaseRecord',
            name: 'releaseRecord',
            meta: { title: '释放记录' },
            component: () => import('../views/release/record'),
          },
        ],
      },

      // 数字资产
      {
        path: '/integral',
        name: 'integral',
        redirect: '/daConfig',
        component: BlankLayout,
        meta: { title: '数字资产', icon: 'el-icon-lx-recharge' },
        children: [
          {
            path: '/daConfig',
            name: 'daConfig',
            meta: { title: '数字资产配置' },
            component: () => import('../views/integral/daConfig'),
          },
          {
            path: '/daLink',
            name: 'daLink',
            meta: { title: '数字资产链信息' },
            component: () => import('../views/integral/daLink'),
          },
          // 三级菜单：资金明细
          {
            path: '/fund',
            name: 'fund',
            redirect: '/cyBer',
            component: BlankLayout,
            meta: { title: '资金明细' },
            children: [
              {
                path: '/cyBer',
                name: 'cyBer',
                meta: { title: '资金明细' },
                component: () => import('../views/integral/fundDetails/cyBer'),
              },
              {
                path: '/recharge',
                name: 'recharge',
                meta: { title: '资金充值审核' },
                component: () => import('../views/integral/fundDetails/fund'),
              },
              {
                path: '/withdraw',
                name: 'withdraw',
                meta: { title: '资金提现审核' },
                component: () => import('../views/integral/fundDetails/withdraw'),
              },
            ],
          },
          {
            path: '/daKind',
            name: 'daKind',
            meta: { title: '数字资产种类', hide: true },
            component: () => import('../views/integral/daKind'),
          },
          {
            path: '/daChain',
            name: 'daChain',
            meta: { title: '数字资产公链配置', hide: true },
            component: () => import('../views/integral/daChain'),
          },
        ],
      },

      // 活动管理
      {
        path: '/activity',
        name: 'activity',
        redirect: '/activity/flashKill',
        component: BlankLayout,
        meta: {
          title: '活动管理',
          hide: true,
          icon: 'el-icon-present',
        },
        children: [
          // 秒杀
          {
            path: '/flashKill',
            name: 'flashKill',
            redirect: '/flashKillCommodity',
            component: BlankLayout,
            meta: { title: '秒杀活动' },
            children: [
              {
                path: '/flashKillCommodity',
                name: 'flashKillCommodity',
                meta: { title: '秒杀商品' },
                component: () => import('../views/activity/flashKill/commodity'),
              },
              {
                path: '/flashKillOrder',
                name: 'flashKillOrder',
                meta: {
                  title: '秒杀订单',
                },
                component: () => import('../views/activity/flashKill/order'),
              },
            ],
          },
          // 幸运大转盘
          {
            path: '/lottery',
            name: 'lottery',
            redirect: '/lotteryCommodity',
            component: BlankLayout,
            meta: { title: '幸运大转盘' },
            children: [
              {
                path: '/lotteryCommodity',
                name: 'lotteryCommodity',
                meta: { title: '幸运大转盘商品' },
                component: () => import('../views/activity/lottery/commodity'),
              },
              {
                path: '/lotteryOrder',
                name: 'lotteryOrder',
                meta: {
                  title: '幸运大转盘订单',
                },
                component: () => import('../views/activity/lottery/order'),
              },
            ],
          },
          // 盲盒
          {
            path: '/blind',
            name: 'blind',
            redirect: '/blindCommodity',
            component: BlankLayout,
            meta: { title: '盲盒活动' },
            children: [
              {
                path: '/blindPartition',
                name: 'blindPartition',
                meta: { title: '盲盒分区' },
                component: () => import('../views/activity/blind/partition'),
              },
              {
                path: '/blindCommodity',
                name: 'blindCommodity',
                meta: { title: '盲盒商品' },
                component: () => import('../views/activity/blind/commodity'),
              },
              {
                path: '/blindOrder',
                name: 'blindOrder',
                meta: {
                  title: '盲盒订单',
                },
                component: () => import('../views/activity/blind/order'),
              },
            ],
          },
        ],
      },

      // 房产管理（隐藏）
      {
        path: '/house',
        name: 'house',
        redirect: '/houseCommodity',
        component: BlankLayout,
        meta: {
          title: '房产管理',
          icon: 'el-icon-office-building',
          hide: true,
        },
        children: [
          {
            path: '/houseCommodity',
            name: 'houseCommodity',
            meta: { title: '房产商品' },
            component: () => import('../views/house/commodity'),
          },
          {
            path: '/houseOrder',
            name: 'houseOrder',
            meta: { title: '房产订单' },
            component: () => import('../views/house/order'),
          },
        ],
      },

      // 装修管理（隐藏）
      {
        path: '/fitment',
        name: 'fitment',
        redirect: '/fitmentCommodity',
        component: BlankLayout,
        meta: {
          title: '装修管理',
          icon: 'el-icon-brush',
          hide: true,
        },
        children: [
          {
            path: '/fitmentClass',
            name: 'fitmentClass',
            meta: { title: '装修分类' },
            component: () => import('../views/fitment/class'),
          },
          {
            path: '/fitmentCommodity',
            name: 'fitmentCommodity',
            meta: { title: '装修商品' },
            component: () => import('../views/fitment/commodity'),
          },
          {
            path: '/fitmentOrder',
            name: 'fitmentOrder',
            meta: { title: '装修订单' },
            component: () => import('../views/fitment/order'),
          },
        ],
      },

      // 交易管理（隐藏）
      {
        path: '/deal',
        name: 'deal',
        redirect: '/fcConfig',
        component: BlankLayout,
        meta: {
          title: '交易管理',
          icon: 'el-icon-s-finance',
          hide: true,
        },
        children: [
          {
            path: '/fcConfig',
            name: 'fcConfig',
            meta: { title: '交易对配置' },
            component: () => import('../views/deal/fcConfig'),
          },
          {
            path: '/fcType',
            name: 'fcType',
            meta: { title: '交易对类型' },
            component: () => import('../views/deal/fcType'),
          },
          {
            path: '/fcOrderUses',
            name: 'fcOrderUses',
            meta: { title: '交易记录' },
            component: () => import('../views/deal/fcOrderUses'),
          },
        ],
      },

      // 其他隐藏页面
      {
        path: '/consumeRank',
        name: 'consumeRank',
        meta: { title: '后台团队配置', hide: true },
        component: () => import('../views/consumeRank'),
      },
      {
        path: '/greenRank',
        name: 'greenRank',
        meta: { title: '后台团队配置', hide: true },
        component: () => import('../views/greenRank'),
      },
      {
        path: '/icon',
        name: 'icon',
        meta: { title: '自定义图标', hide: true },
        component: () => import('../views/Icon.vue'),
      },
      {
        path: '/404',
        name: '404',
        meta: { title: '找不到页面', hide: true },
        component: () => import('../views/404.vue'),
      },
      {
        path: '/403',
        name: '403',
        meta: { title: '没有权限', hide: true },
        component: () => import('../views/403.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录' },
    component: () => import('../views/Login.vue'),
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {
  document.title = ` 禧易宝 | ${to.meta.title}`
  const role = sessionStorage.getItem('Token')

  if (!role && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})

export default router
